package com.hnjd.headline.controller;

import com.hnjd.headline.pojo.User;
import com.hnjd.headline.service.UserService;
import com.hnjd.headline.utils.Constant;
import com.hnjd.headline.utils.Result;
import com.hnjd.headline.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user 用户信息必须包含(username, userPwd)
     * @return if success, return 200 token, else return 501/503 error message
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if (user.getUsername() == null ||
                user.getUsername().isEmpty() ||
                user.getUserPwd() == null ||
                user.getUserPwd().isEmpty()
        ) {
            return Result.build(ResultCodeEnum.BAD_REQUEST);
        }

        String token = userService.login(user);

        if (token == null) {
            return Result.build(ResultCodeEnum.USERNAME_ERROR);
        } else if (token.isEmpty()) {
            return Result.build(ResultCodeEnum.PASSWORD_ERROR);
        } else {
            Map<String, String> data = Map.of("token", token);
            return Result.ok(data);
        }
    }

    /**
     * 获取用户信息
     * @param token token
     * @return loginUser
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader(Constant.REQUEST_HEAD_TOKEN_KEY) String token) {
        if (token == null || token.isEmpty()) {
            return Result.build(ResultCodeEnum.NOTLOGIN);
        }

        User user = userService.getUserByToken(token);
        if (user == null) {
            return Result.build(ResultCodeEnum.NOTLOGIN);
        }

        // 数据对齐
        Map<String, Object> data = Map.of("loginUser", user);

        return Result.ok(data);
    }

    /**
     * 检查用户名是否已被使用
     * @param username 用户名
     * @return if success, return 200 null, else return error 505 message
     */
    @PostMapping("/checkUserName")
    public Result checkUserName(String username) {
        if (username == null || username.isEmpty()) {
            return Result.build(ResultCodeEnum.BAD_REQUEST);
        }

        Boolean isUsed = userService.checkUserNameUsed(username);

        if (isUsed) {
            return Result.build(ResultCodeEnum.USERNAME_USED);
        } else {
            return Result.ok();
        }
    }

    /**
     * 注册用户
     * @param user 用户信息 必须包含(username, userPwd, nickName)
     * @return if success, return 200 null, else return error message
     */
    @PostMapping("/regist")
    public Result register(@RequestBody User user) {
        // 校验参数
        if (user.getUsername() == null || user.getUsername().isEmpty() ||
                user.getUserPwd() == null || user.getUserPwd().isEmpty() ||
                user.getNickName() == null || user.getNickName().isEmpty()) {
            return Result.build(ResultCodeEnum.BAD_REQUEST);
        }

        // 判断用户名是否已被使用
        Boolean isUsed = userService.checkUserNameUsed(user.getUsername());
        if (isUsed) {
            return Result.build(ResultCodeEnum.USERNAME_USED);
        }

        // 注册用户
        int rows = userService.register(user);
        if (rows == 0) {
            throw new RuntimeException("注册失败");
        } else {
            return Result.ok();
        }
    }

    /**
     * 检查登录状态
     * @param token token
     * @return if success, return 200 null, else return 504 error message
     */
    @GetMapping("/checkLogin")
    public Result checkLogin(@RequestHeader(Constant.REQUEST_HEAD_TOKEN_KEY) String token) {
        // 判空
        if (token == null || token.isEmpty()) {
            return Result.build(ResultCodeEnum.NOTLOGIN);
        }
        // 判过期
        if (userService.checkTokenExpiration(token)) {
            return Result.build(ResultCodeEnum.NOTLOGIN);
        }
        // 成功
        return Result.ok();
    }
}
